home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / GRAPHICS / SPRite_TOOLS.ARC / c / spr2tga < prev    next >
Text File  |  1998-04-03  |  5KB  |  169 lines

  1. /************************************************************************
  2.  *                                    *
  3.  * spr2tga.c                                *
  4.  *                                    *
  5.  * Sprite to TGA 16 bit format                        *
  6.  * using 2nd generation sprite routines                    *
  7.  *                                    *
  8.  * Version 2.00 (22-Aug-1993)                        *
  9.  *                                    *
  10.  * (C) 1993 DEEJ Technology PLC                        *
  11.  *                                    *
  12.  ************************************************************************/
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "io.h"
  18. #include "sprite.h"
  19. #include "tga.h"
  20.  
  21.  
  22. int main(int argc, char **argv)
  23. {
  24.         FILE *inf, *outf, *errf;
  25.         int          i,x,y,Y;
  26.         int          r,g,b;
  27.         int          bpp;
  28.         DWORD        rgb;
  29.         WORD         val;
  30.         BYTE        *tga_buf;
  31.         BYTE        *buf_ptr;
  32.         int          buf_size;
  33.         spr_info_str spr;
  34.         tga_hdr_str  tga;
  35.         char         string[256];
  36.  
  37.         if(argc>1 && argv[1][0]=='-')
  38.         {
  39.                 bpp = atoi(&argv[1][1]);
  40.  
  41.                 if(bpp != 0)
  42.                 {
  43.                         if(bpp!=8 && bpp!=16 && bpp!=24)
  44.                         {
  45.                                 fprintf(errf,"Bad bpp for TGA image\n");
  46.                                 return(1);
  47.                         }
  48.                         argc--; argv++;
  49.                 }
  50.         }
  51.         else
  52.         {
  53.                 bpp = 0;
  54.         }
  55.  
  56.         file_args(argc, argv, &inf, &outf, &errf);
  57.  
  58.         read_sprite(&spr, inf);
  59.  
  60.         if(bpp==0 || bpp<spr.bpp)
  61.         {
  62.                 switch(spr.bpp)
  63.                 {
  64.                         case 15: bpp=16; break;
  65.                         case 24: bpp=24; break;
  66.                         default: bpp=8;  break;
  67.                 }
  68.         }
  69.  
  70.         Y = spr.Y * spr.Yasp;
  71.  
  72.         tga.IDLength        = 0;
  73.         tga.MapType         = bpp==8 ? 1:0;
  74.         tga.ImageType       = bpp==8 ? 1:2;
  75.         tga.MapInfoOrgin    = 0;
  76.         tga.MapInfoLength   = bpp==8 ? 256:0;
  77.         tga.MapInfoWidth    = bpp==8 ? 24:0;
  78.         tga.ImageInfoX      = 0;
  79.         tga.ImageInfoY      = 0;
  80.         tga.ImageInfoDX     = spr.X;
  81.         tga.ImageInfoDY     = Y;
  82.         tga.ImageInfoDepth  = bpp;
  83.         tga.ImageInfoAttrib = 0;
  84.  
  85.         buf_size = spr.X * (bpp/8);
  86.  
  87.         if((tga_buf = malloc(buf_size)) == 0)
  88.         {
  89.                 fprintf(errf,"Unable to allocate TGA buffer\n");
  90.                 exit(1);
  91.         }
  92.  
  93.         write_struct(LE, (BYTE*)&tga, tga_hdr_descr, outf);
  94.  
  95.         sprintf(string,"Generating TGA %dx%dx%d:",tga.ImageInfoDX,
  96.                                                   tga.ImageInfoDY,
  97.                                                   tga.ImageInfoDepth);
  98.         progress_start(string);
  99.  
  100.         /* write colour map if 8 bpp */
  101.  
  102.         if(bpp==8)
  103.         {
  104.                 if(spr.has_palette==FALSE) default_palette(&spr);
  105.  
  106.                 for(i=0; i<(int)tga.MapInfoLength; i++)
  107.                 {
  108.                         if(i<spr.cols)
  109.                         {
  110.                                 r = (spr.palette[i] >>  8) & 0xFF;
  111.                                 g = (spr.palette[i] >> 16) & 0xFF;
  112.                                 b = (spr.palette[i] >> 24) & 0xFF;
  113.                         }
  114.                         else
  115.                         {
  116.                                 r = g = b = 0;
  117.                         }
  118.                         fputc(b, outf);
  119.                         fputc(g, outf);
  120.                         fputc(r, outf);
  121.                 }
  122.         }
  123.  
  124.         for(y=Y-1; y>=0; y--)
  125.         {
  126.                 buf_ptr = tga_buf;
  127.  
  128.                 for(x=0; x<spr.X; x++)
  129.                 {
  130.                         switch(bpp)
  131.                         {
  132.                         case 8:
  133.                                 val = read_pixel_val(&spr, x, y/spr.Yasp);
  134.                                 *buf_ptr++ = (char)val;
  135.                                 break;
  136.  
  137.                         case 16:
  138.                                 rgb = read_pixel_RGB(&spr, x, y/spr.Yasp);
  139.  
  140.                                 /* convert %BBBBBBBBGGGGGGGGRRRRRRRR00000000 */
  141.                                 /* to      %0RRRRRGGGGGBBBBB                 */
  142.  
  143.                                 val = (((rgb>>27) & 0x1F) << 0) +
  144.                                       (((rgb>>19) & 0x1F) << 5) +
  145.                                       (((rgb>>11) & 0x1F) << 10);
  146.  
  147.                                 buf_ptr[0] = (char)val;
  148.                                 buf_ptr[1] = (char)(val>>8);
  149.                                 buf_ptr   += 2;
  150.                                 break;
  151.  
  152.                         case 24:
  153.                                 rgb = read_pixel_RGB(&spr, x, y/spr.Yasp);
  154.                                 buf_ptr[0] = (rgb >> 24);
  155.                                 buf_ptr[1] = (rgb >> 16);
  156.                                 buf_ptr[2] = (rgb >>  8);
  157.                                 buf_ptr   += 3;
  158.                                 break;
  159.                         }
  160.                 }
  161.                 fwrite(tga_buf, buf_size, 1, outf);
  162.  
  163.                 progress(Y-y,Y);
  164.         }
  165.         progress_finish();
  166.  
  167.         return(0);
  168. }
  169.